package servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import pojo.Product;
import service.ProductManager;
import service.WarehouseManager;
import service.impl.ProductManagerImpl;
import service.impl.WarehouseManagerImpl;

/**
 * Servlet implementation class MyWarehouseServlet
 */
@WebServlet("/sec/MyWarehouseServlet")
public class ProductListShowServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	private ProductManager manager;
    
    public ProductListShowServlet() {
        super();
        this.manager = new ProductManagerImpl();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Logger logger = Logger.getLogger(ProductListShowServlet.class);
	
		String wid  = request.getParameter("ww");
		String pname=  request.getParameter("pname");
		String pprice = request.getParameter("pprice");
		String pcount = request.getParameter("pcount");
		String pdate = request.getParameter("pdate");
		

		
		
		///校验：
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		///测试输出
		System.out.println(pdate);
		try {
			System.out.println(format.parse(pdate));
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		boolean isOk = true;
		if(pname==null||pname.trim().length()==0){
			request.setAttribute("err_pname", "货品名为空");
			isOk = false;
		}else{
			request.setAttribute("err_pname", null);
		}
		if(pprice==null || pprice.trim().length()==0){
			request.setAttribute("err_pname", "货品价格为空");
			isOk = false;
		}
		else if(pprice.length()>8){
			request.setAttribute("err_pname", "货品价格不能大于8位");
			isOk = false;
		}else{
			request.setAttribute("err_pname", null);
		}
		
		if(pcount==null || pcount.trim().length()==0){
			request.setAttribute("err_pname", "货品数量不能为空，至少为1");
			isOk = false;
		}
		
		if(!isOk){
			request.getRequestDispatcher("edit.jsp").forward(request, response);
			return;
		}
		
		Product p = new Product();
		double price = Double.parseDouble(pprice);
		int count = Integer.parseInt(pcount);
		Date date = null;
		try {
			date = format.parse(pdate);
		} catch (ParseException e) {
			
			e.printStackTrace();
			request.setAttribute("err_pdate", "日期格式有误");
			isOk = false;
		}
		
		p.setName(pname);
		p.setPrice(price);
		p.setCount(count);
		p.setPd(date);
		p.setWid(wid);
		
		System.out.println("要添加进入的Pro： "+ p);
		
		if(this.manager.addProduct(p)>0){
			request.setAttribute("curw", wid);
			request.getRequestDispatcher("listupdate").forward(request, response);
	
		}
		
		
	}

}
